package com.moli.iov.down.command.codec.decode.decoders;

import com.moli.iov.down.command.codec.decode.bean.RemoteControlResResultBean;
import com.moli.iov.down.command.codec.utils.ProtocolAnalysisUtil;
import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * <p>
 *  远程车辆控制命令应答 解码
 * </p>
 *
 * @author: zql
 * @Date: 2018/11/30 17:52
 */
@Component("remoteControlDecoder")
@Scope("prototype")
public class RemoteControlDecoder extends BaseDecoder{

    protected static final Logger log = LoggerFactory.getLogger(ProtocolAnalysisUtil.class);

    public RemoteControlResResultBean decode(byte[] data){
        log.info("远程车辆控制命令应答 解码 入参：{}", data);
        byte[] controlCommand = ArrayUtils.subarray(data, 0, 1);
        data = ArrayUtils.subarray(data, 1, data.length);

        byte[] date = ArrayUtils.subarray(data, 0, 6);
        data = ArrayUtils.subarray(data, 6, data.length);

        byte[] serialNumber = ArrayUtils.subarray(data, 0, 2);
        data = ArrayUtils.subarray(data, 2, data.length);

        byte[] result = ArrayUtils.subarray(data, 0, 1);
        data = ArrayUtils.subarray(data, 1, data.length);

        byte[] errorCode = ArrayUtils.subarray(data, 0, 1);


        RemoteControlResResultBean remoteControlResBean = new RemoteControlResResultBean();
        remoteControlResBean.setDate(this.dateDecode(date));
        remoteControlResBean.setSerialNumber(this.serialNumberDecode(serialNumber));
        remoteControlResBean.setControlCommand(controlCommand[0]);
        remoteControlResBean.setResult(result[0]);
        remoteControlResBean.setFailCode(errorCode[0]);
        log.info("远程车辆控制命令应答 解码 结果: {}", remoteControlResBean);
        return remoteControlResBean;
    }
}
